package td;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TD {

	public static final Integer SIX_COMB = 6;
	public static final Integer THR_COMB = 3;
	public static final Integer ONE_COMB = 1;
	public static final Integer ERR_COMB = 0;

	private Integer code;
	private List<Integer> numbers = new ArrayList<Integer>();

	public TD() {
		super();
	}

	public TD(List<Integer> numbers) {
		super();
		this.numbers = numbers;
		computeCode();
	}

	public TD(Integer code) {
		super();
		this.code = code;
		computeNumbers();
	}

	private void computeNumbers() {
		if (code != null) {
			numbers.add(code / 100);
			numbers.add((code % 100) / 10);
			numbers.add(code % 10);
		}
	}

	private void computeCode() {
		if (this.numbers != null && this.numbers.size() == 3) {
			this.code = 100 * this.numbers.get(0) + 10 * this.numbers.get(1)
					+ this.numbers.get(2);
		}
	}

	public int getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
		computeNumbers();
	}

	public List<Integer> getNumbers() {
		return numbers;
	}

	public void setNumbers(List<Integer> numbers) {
		this.numbers = numbers;
		computeCode();
	}

	public Integer type() {
		if (this.numbers == null || this.numbers.size() != 3) {
			return ERR_COMB;
		}
		int ff = 0;
		if (this.numbers.get(0) == this.numbers.get(1)) {
			ff++;
		}
		if (this.numbers.get(0) == this.numbers.get(2)) {
			ff++;
		}
		if (this.numbers.get(2) == this.numbers.get(1)) {
			ff++;
		}
		return ff == 3 ? ONE_COMB : (ff == 1 ? THR_COMB : SIX_COMB);
	}

	public TD combTD() {
		TD td = new TD(this.code);
		Collections.sort(td.getNumbers());
		td.computeCode();
		return td;
	}

	public int contain(TD td) {
		int count = 0;
		if (td != null) {
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 3; j++) {
					if (td.getNumbers().get(j) == numbers.get(i)) {
						count++;
					}
				}
			}
		}
		return count;
	}
}
